//
//  LoginViewController.m
//  Pink
//
//  Created by ZhongDaYunKe on 15/12/11.
//  Copyright © 2015年 ycy. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>


@property(nonatomic,strong) TPKeyboardAvoidingScrollView *scrollView;
@property(nonatomic,strong)UITextField *userNameField;
@property(nonatomic,strong)UITextField *passWordField;
 @property(nonatomic,strong)UIButton *forgetBtn;
@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)UIButton *registBtn;
@property(nonatomic,strong)UIButton *changeBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:self.view.frame];
    _scrollView.scrollEnabled = NO;
    [self.view addSubview:_scrollView];
//    _scrollView.contentSize = [UIScreen mainScreen].bounds.size;
//    WS(weakself)
//    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(weakself.view);
//        make.size.mas_equalTo(weakself.view.size);
//    
//    }];
    
    WS(weakself);
    _changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_changeBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _changeBtn.frame = CGRectMake(0, 0, 37, 25);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_changeBtn];
    [_changeBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        
        if (_registBtn.alpha == 0) {
            [sender setTitle:@"登录" forState:UIControlStateNormal];
            weakself.title = @"注册";
            weakself.loginBtn.easeInEaseOut.alpha = 0;
            weakself.loginBtn.duration = 0.5;
            weakself.registBtn.duration = 0.5;
            weakself.registBtn.easeInEaseOut.alpha = 1;
            weakself.forgetBtn.duration = 0.5;
            weakself.forgetBtn.easeInEaseOut.alpha = 0;
        }else{
            [sender setTitle:@"注册" forState:UIControlStateNormal];
            weakself.title = @"登录";
            weakself.loginBtn.easeInEaseOut.alpha = 1;
            weakself.loginBtn.duration = 0.5;
            weakself.registBtn.duration = 0.5;
            weakself.registBtn.easeInEaseOut.alpha = 0;
            weakself.forgetBtn.duration = 0.5;
            weakself.forgetBtn.easeInEaseOut.alpha = 1;
        }
        
        
    }];
    
    self.title = @"登录";
   [self initSubViews];
    
    
    
    
      // Do any additional setup after loading the view from its nib.
}

- (void)initSubViews
{
     WS(weakself)
    
    UIImageView *backGroundImageView = [[UIImageView alloc] init];
    
    backGroundImageView.userInteractionEnabled = YES;
    backGroundImageView.image = [UIImage imageNamed:@"LoginImageBack"];
    backGroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    [_scrollView addSubview:backGroundImageView];
    
    [backGroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(weakself.scrollView.size);
    }];
    
    UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LoginLogo"]];
    [backGroundImageView addSubview:logoView];
    [logoView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(weakself.view.mas_centerX);
        make.top.mas_equalTo(weakself.view.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(116, 36));
    }];
    
    _userNameField = [[UITextField alloc] init];
    [backGroundImageView addSubview:_userNameField];
    _userNameField.tag = 20;
    
    _passWordField = [[UITextField alloc] init];
    [backGroundImageView addSubview:_passWordField];
    _passWordField.tag = 21;
    
    [_userNameField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakself.view.mas_centerX);
        make.top.mas_equalTo(logoView.mas_bottom).offset(50);
         make.left.mas_equalTo(weakself.view.mas_left).offset(20);
        make.right.mas_equalTo(weakself.view.mas_right).offset(-20);
        make.height.mas_equalTo(35);
        
        
    }];
    
    [_passWordField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakself.view.mas_centerX);
        make.top.mas_equalTo(weakself.userNameField.mas_bottom).offset(2);
        make.height.mas_equalTo(35);
        make.left.mas_equalTo(weakself.view.mas_left).offset(20);
        make.right.mas_equalTo(weakself.view.mas_right).offset(-20);

        
    }];
    
    
    _forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_forgetBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [_forgetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backGroundImageView addSubview:_forgetBtn];
    _forgetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.passWordField.mas_right);
        make.top.mas_equalTo(weakself.passWordField.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    //忘记密码
    [_forgetBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
       
        
    }];
    
    
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_loginBtn setBackgroundImage:[UIImage imageNamed:@"LoginButton"] forState:UIControlStateNormal];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backGroundImageView addSubview:_loginBtn];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(weakself.passWordField.mas_bottom).offset(40);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(weakself.view.mas_left).offset(20);
        make.right.mas_equalTo(weakself.view.mas_right).offset(-20);
        
    }];
    [_loginBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        
        [WSProgressHUD showShimmeringString:@"登录中"];
        if (weakself.userNameField.text.length == 0 || weakself.passWordField.text.length == 0) {
            [WSProgressHUD showErrorWithStatus:@"请输入完整"];
            
        }else{
            [AVUser logInWithUsernameInBackground:weakself.userNameField.text password:weakself.passWordField.text block:^(AVUser *user, NSError *error) {
                if (user) {
                    [WSProgressHUD showSuccessWithStatus:@"登录成功"];
                    
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [weakself.navigationController popViewControllerAnimated:YES];
                    });
                    
                }else{
                    [WSProgressHUD showErrorWithStatus:error.description];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [WSProgressHUD dismiss];
                    });
                    
                }
                
            }];
        }
        
        
    }];
    
    
    _registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registBtn setBackgroundImage:[UIImage imageNamed:@"registerButton"] forState:UIControlStateNormal];
    [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_registBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backGroundImageView addSubview:_registBtn];
    [_registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakself.passWordField.mas_bottom).offset(40);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(weakself.view.mas_left).offset(20);
        make.right.mas_equalTo(weakself.view.mas_right).offset(-20);
    }];
    _registBtn.alpha = 0;
    [_registBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        
        
        [WSProgressHUD showShimmeringString:@"注册中"];
        if (weakself.userNameField.text.length == 0 || weakself.passWordField.text.length == 0) {
            [WSProgressHUD showErrorWithStatus:@"请输入完整"];
            
        }else{
            
            AVUser *user = [AVUser user];
            user.username = weakself.userNameField.text;
            user.password = weakself.passWordField.text;
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    [WSProgressHUD showSuccessWithStatus:@"注册成功"];
                    
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [weakself.navigationController popViewControllerAnimated:YES];
                    });
                    
                }else{
                    [WSProgressHUD showErrorWithStatus:error.description];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [WSProgressHUD dismiss];
                    });
                    
                }

            }];
        }

    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self modifytextField:_userNameField];
        [self modifytextField:_passWordField];
     });
    
    
    UIButton *weibo  = [UIButton buttonWithType:UIButtonTypeCustom];
    [weibo setBackgroundImage:[UIImage imageNamed:@"weibo"] forState:UIControlStateNormal];
    [backGroundImageView addSubview:weibo];
    [weibo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakself.view.mas_centerX).offset(-60);
        make.top.mas_equalTo(weakself.loginBtn.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(46, 46));
    }];
    [weibo addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
       
        
        
    }];
    
    
    
    UIButton *weixin  = [UIButton buttonWithType:UIButtonTypeCustom];
    [weixin setBackgroundImage:[UIImage imageNamed:@"weixin"] forState:UIControlStateNormal];
    [backGroundImageView addSubview:weixin];
    [weixin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakself.view.mas_centerX).offset(60);
        make.top.mas_equalTo(weakself.loginBtn.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(46, 46));
    }];
    [weixin addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        
        
        
    }];

    
    
    
}


-(void)modifytextField:(UITextField *)textfield
{
    textfield.delegate = self;
    textfield.font = [UIFont systemFontOfSize:13];
    textfield.textColor = [UIColor whiteColor];
    
    NSInteger index = textfield.tag - 20;
    if (index > 0) {
        textfield.secureTextEntry = YES;
    }
    NSArray *placeholdertext = @[@"用户名",@"密码",@"确认密码"];
    NSArray *leftViewImage = @[@"UsernameLogo",@"password",@"confirmPassword"];
    textfield.placeholder = placeholdertext[index];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:leftViewImage[index]]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.frame = CGRectMake(9, 8, 18, 16);
    
    UIView *Leftview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 32)];
    [Leftview addSubview:imageView];
    
    textfield.leftViewMode = UITextFieldViewModeAlways;
    textfield.leftView = Leftview;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor whiteColor];
    [textfield addSubview:lineView];
    lineView.frame = CGRectMake(0, CGRectGetMaxY(textfield.bounds)-0.5, textfield.size.width, 0.5);
    
    
}

-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);  //size 为CGSize类型，即你所需要的图片尺寸
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
